package com.xlh.service.courseTest.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.xlh.dao.courseTest.helper.CourseTestPaperClassMapperHelper;
import com.xlh.dto.courseTest.PaperListDataDTO;
import com.xlh.pojo.courseTest.CourseTestPaperClass;
import com.xlh.pojo.user.Class;
import com.xlh.service.courseTest.CourseTestPaperClassService;
import com.xlh.service.user.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CourseTestPaperClassServiceImpl implements CourseTestPaperClassService {


    @Autowired
    private CourseTestPaperClassMapperHelper courseTestPaperClassMapperHelper;

    @Autowired
    private ClassService classService;


    @Override
    public void setClassName(List<PaperListDataDTO> list) {
        // 对于班级的赋值
        List<Long> paperIds = list.stream().map(PaperListDataDTO::getPaperId).collect(Collectors.toList());
        List<CourseTestPaperClass> courseTestPaperClasses = courseTestPaperClassMapperHelper.listByPaperIds(paperIds);
        Map<Long, List<CourseTestPaperClass>> paperIdMapClassIds = courseTestPaperClasses.stream().collect(Collectors.groupingBy(CourseTestPaperClass::getPaperId));

        List<Long> classIds = courseTestPaperClasses.stream().map(CourseTestPaperClass::getClassId).distinct().collect(Collectors.toList());
        List<Class> classes = classService.listClass(classIds, false);
        Map<Long, List<Class>> classIdMapClassName = classes.stream().collect(Collectors.groupingBy(Class::getId));

        for (int i = 0; i <list.size(); i++) {

            PaperListDataDTO paperListDataDTO = list.get(i);
            List<Class> paperIdClassNames = new ArrayList<>();
            Long paperId = paperListDataDTO.getPaperId();

            List<CourseTestPaperClass> courseTestPaperClassList = paperIdMapClassIds.get(paperId);
            if(CollectionUtil.isNotEmpty(courseTestPaperClassList)){
                List<Long> classIdsOfPaperId = courseTestPaperClassList.stream().map(CourseTestPaperClass::getClassId).collect(Collectors.toList());
                for (int j = 0; j <classIdsOfPaperId.size(); j++) {

                    List<Class> classesOfPaperId = classIdMapClassName.get(classIdsOfPaperId.get(j));
                    if(CollectionUtil.isNotEmpty(classesOfPaperId)){
                        Class aClass = classesOfPaperId.get(0);
                        paperIdClassNames.add(aClass);
                    }
                }
            }
            String classNames = paperIdClassNames.stream().map(Class::getName).collect(Collectors.joining(","));
            paperListDataDTO.setClassName(classNames);
        }

    }

    @Override
    public List<Long> getClassByPaperId(List<Long> ids) {

        List<CourseTestPaperClass> courseTestPaperClasses = courseTestPaperClassMapperHelper.listByPaperIds(ids);
        return courseTestPaperClasses.stream().map(CourseTestPaperClass::getClassId).collect(Collectors.toList());
    }

    @Override
    public Boolean isContains(List<Long> paperIds, Long classId) {
        List<CourseTestPaperClass> courseTestPaperClassList = courseTestPaperClassMapperHelper.listByPaperIds(paperIds);
        if(CollectionUtil.isEmpty(courseTestPaperClassList)){
            return false;
        }
        List<Long> classIds = courseTestPaperClassList
                .stream()
                .map(CourseTestPaperClass::getClassId)
                .distinct().collect(Collectors.toList());

        return classIds.contains(classId);
    }

    @Override
    public List<Long> delClassAndGetDelPaperIds( List<Long> paperIds,
                                                 List<Long> classIds ) {

        List<CourseTestPaperClass> courseTestPaperClasses = courseTestPaperClassMapperHelper.listByClassIds(classIds);
        List<Long> classesPaperIds = courseTestPaperClasses.stream().map(CourseTestPaperClass::getPaperId).collect(Collectors.toList());
        List<CourseTestPaperClass> courseTestPaperClassList = courseTestPaperClassMapperHelper.listByPaperIds(classesPaperIds);

        Map<Long, List<CourseTestPaperClass>> map = courseTestPaperClassList.stream().collect(Collectors.groupingBy(CourseTestPaperClass::getPaperId));
        List<Long> delPaperIds = new ArrayList<>();

        for (Long paperId : map.keySet()) {
            List<CourseTestPaperClass> paperIdMapClass = map.get(paperId);
            if(paperIdMapClass.size() == 1){
                delPaperIds.add(paperId);
            }
        }
        return delPaperIds;
    }

}
